package com.hoshiicloud.goods.controller;

import com.hoshiicloud.auth.vo.TokenUsersVO;
import com.hoshiicloud.common.rpc.Response;
import com.hoshiicloud.goods.param.goods.GoodsReleaseSpuParam;
import com.hoshiicloud.goods.service.GoodsReleaseService;
import com.hoshiicloud.goods.feign.UpLoadFileService;
import com.hoshiicloud.goods.utils.UserInfoUtil;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;

@Slf4j
@Validated
@RestController
@RequestMapping("/goods/goods-release")
@Api(value = "商品发布", tags = {"商品发布"})
public class GoodsReleaseController {

    @Autowired
    private GoodsReleaseService goodsReleaseService;

    @ApiOperation(value = "根据类目id,查询相关信息")
    @GetMapping("/query")
    public Response queryMessage(@RequestParam Long categoryId, HttpServletRequest request) {
        Map<String, Object> myMap = new HashMap<>();
        TokenUsersVO user = UserInfoUtil.getUserInfo(request);
        myMap.put("companyId", user.getCompanyId());
        myMap.put("platformId", user.getPlatId());
        myMap.put("storeId", user.getStoreId());
        return Response.data(goodsReleaseService.queryMessage(categoryId,myMap));
    }

    @ApiOperation(value = "发布商品")
    @PostMapping("/saveGoods")
    public Response saveGoods(@RequestBody GoodsReleaseSpuParam goodsReleaseSpuParam, HttpServletRequest rq) {

        TokenUsersVO user = UserInfoUtil.getUserInfo(rq);
        goodsReleaseSpuParam.setCompanyId(user.getCompanyId());
        goodsReleaseSpuParam.setPlatformId(user.getPlatId());
        goodsReleaseSpuParam.setStoreId(user.getStoreId());

        // 创建人
        goodsReleaseSpuParam.setCreateBy(user.getRealName());
        return Response.data(goodsReleaseService.saveGoods(goodsReleaseSpuParam));
    }

    @ApiOperation(value = "查询需要编辑的商品信息")
    @PostMapping("/getNeedEditGoods")
    public Response getNeedEditGoods(@RequestBody Map<String, Object> map, HttpServletRequest rq) {

        TokenUsersVO user = UserInfoUtil.getUserInfo(rq);
        map.put("companyId", user.getCompanyId());
        map.put("platformId", user.getPlatId());
        map.put("storeId", user.getStoreId());

        return Response.data(goodsReleaseService.getNeedEditGoods(map));
    }

    @ApiOperation(value = "编辑商品")
    @PostMapping("/editGoods")
    public Response editGoods(@RequestBody GoodsReleaseSpuParam goodsReleaseSpuParam, HttpServletRequest rq) {

        // 店铺平台等信息
        TokenUsersVO user = UserInfoUtil.getUserInfo(rq);
        goodsReleaseSpuParam.setCompanyId(user.getCompanyId());
        goodsReleaseSpuParam.setPlatformId(user.getPlatId());
        goodsReleaseSpuParam.setStoreId(user.getStoreId());
        goodsReleaseSpuParam.setUpdateBy(user.getRealName());

        if (goodsReleaseSpuParam.getEditType() == 1) {
            // 编辑总店商品
            return Response.data(goodsReleaseService.editGoods(goodsReleaseSpuParam));
        }else if(goodsReleaseSpuParam.getEditType() == 2){
            // 编辑总店商品的同时保存至分店
            return Response.data(goodsReleaseService.editAllGoods(goodsReleaseSpuParam));
        }else {
            return Response.failed("编辑失败，无此选项");
        }

    }

    /**
     * 文件上传
     */
    @RequestMapping(value = "/uploadFile", method = RequestMethod.POST,consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
    public Response uploadFile(@RequestPart MultipartFile file) {
        return goodsReleaseService.uploadFile(file);
    }

}
